package com.lz.ui_lib.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * 流式布局
 * <p>
 * Created by Liuyk on 2015/9/3.
 */
public class FlowLayout extends ViewGroup {
    private final String TAG = "position:";

    public FlowLayout(Context context) {
        this(context, null);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.i(TAG, "onMeasure");

        final int layoutWidth = MeasureSpec.getSize(widthMeasureSpec);
        final int layoutWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int layoutHeight = MeasureSpec.getSize(widthMeasureSpec);
        final int layoutHeightMode = MeasureSpec.getMode(widthMeasureSpec);
        Log.i(TAG, layoutWidth + ":" + layoutHeight);

        //计算出在warp_content下FlowLayout的宽高
        int measureWidth = 0;//warp_content下FlowLayout的宽
        int measureHeight = 0;//warp_content下FlowLayout的高
        //每一行的宽度
        int lineWidth = 0;
        //每一行的最大高度
        int lineMaxHeight = 0;
        for (int i = 0; i < getChildCount(); i++) {
            //测量某一个子View
            final View childView = getChildAt(i);
            final MarginLayoutParams params = (MarginLayoutParams) childView.getLayoutParams();
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);

            //获得每一个子View的宽高
            final int childWidth = childView.getMeasuredWidth() + params.leftMargin + params.rightMargin;
            final int childHeight = childView.getMeasuredHeight() + params.topMargin + params.bottomMargin;

            if (childWidth + lineWidth > layoutWidth) {//换行
                measureWidth = Math.max(measureWidth, lineWidth);

                measureHeight = measureHeight + lineMaxHeight;
                lineWidth = 0;
                lineMaxHeight = 0;
            }

            lineMaxHeight = Math.max(lineMaxHeight, childHeight);
            lineWidth = lineWidth + childWidth;

            if (i == getChildCount() - 1) {//这种情况下,最后一个View将无法被统计
                measureHeight = measureHeight + lineMaxHeight;
            }

        }
        //设置FlowLayout的宽高
        measureWidth = layoutWidthMode == MeasureSpec.EXACTLY ? layoutWidth : measureWidth;
        measureHeight = layoutHeightMode == MeasureSpec.EXACTLY ? layoutHeight : measureHeight;
        Log.i(TAG, "measureWidth:" + measureWidth);
        Log.i(TAG, "measureHeight:" + measureHeight);
        setMeasuredDimension(measureWidth, heightMeasureSpec);
        super.onMeasure(MeasureSpec.makeMeasureSpec(measureWidth, layoutWidthMode),
                MeasureSpec.makeMeasureSpec(measureHeight, layoutHeightMode));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //layout(10, 10, 100, 100) 在(10,10)处显示,显示大小为:90,90

        //当前FlowLayout的宽度
        final int layoutWidth = getMeasuredWidth();

        //当前行的总宽度
        int lineWidth = 0;
        //所有行的总高度
        int lineHeight = 0;
        //当前行的最大高度
        int maxLineHeight = 0;
        for (int i = 0; i < getChildCount(); i++) {
            final View childView = getChildAt(i);
            final MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();

            int childWidth = childView.getMeasuredWidth() + params.leftMargin + params.rightMargin;
            int childHeight = childView.getMeasuredHeight() + params.topMargin + params.bottomMargin;
            int cRight;
            int cBottom;

//            if(i == 0){
//                lineHeight = params.topMargin;
//            }

            if (lineWidth == 0) {//使得没一行的第一个元素,margin有效
                lineWidth = params.leftMargin;
//                lineHeight = lineHeight + params.topMargin;
            }

            cRight = lineWidth + childView.getMeasuredWidth();
            cBottom = lineHeight + childView.getMeasuredHeight();
            childView.layout(lineWidth, lineHeight, cRight, cBottom);

            //将当前行的总宽度进行叠加
            lineWidth = lineWidth + childWidth;

            if (lineWidth + childWidth > layoutWidth) {//换行
                //初始化当前行的值
                lineWidth = 0;
                //将行的总高度进行叠加
                lineHeight = lineHeight + maxLineHeight;
                maxLineHeight = 0;
            }
            maxLineHeight = Math.max(maxLineHeight, childHeight);

            Log.i(TAG, lineHeight + ":" + childHeight + ":" + cRight + ":" + cBottom);
        }
    }

    //与当前ViewGroup对应的LayoutParams
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }
}
